package com.example.dps;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
	WebHelper webHelper;
	
	DBHelper dbHelper;
	
	
	@Override
	public void onBackPressed() {

		Intent i = new Intent();

		i.setClass(this, MainActivity.class);

		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

		startActivity(i);
	}
	
	
	@Override
	public void onConfigurationChanged(Configuration newConfig){
		super.onConfigurationChanged(newConfig);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		
		Utility.adjustBackground(this);
		

		addButtonListeners();
		
		this.webHelper = new WebHelper(this);
		
		this.dbHelper = new DBHelper(this);
		
		/**
		 * do remember me check and actions
		 */
		CheckBox rememberMeCheck = (CheckBox) findViewById(R.id.rememberMeCheckBox);
		if(this.dbHelper.getBoolean(DBHelper.COLUMN_REMEMBER_ME)){
			
			
			rememberMeCheck.setChecked(true);
			
			//if remember me, fill in username
			String username = this.dbHelper.getUsername();
			
			if(username != null){
				EditText loginUsername = (EditText) findViewById(R.id.loginUsernameValue);
				loginUsername.setText(username);
			}
		}
		else{
			rememberMeCheck.setChecked(false);
		}
		
		
		
	}
	
	public void onAuthenticate(String username, boolean loggedIn){
		if(loggedIn){
			
			/** update username in table **/
			LoginActivity.this.dbHelper.setUsername(username);
			LoginActivity.this.dbHelper.setBoolean(DBHelper.COLUMN_ACCOUNT_CREATED, 1);
			
			//remember username if remember me is checked

			
			//LoginActivity.this.webHelper.getAccountInfo();
			
			/**start next activity, with call back to original  activity (passed through START_TAB) **/
		
			
			
			Bundle extras = LoginActivity.this.getIntent().getExtras();
			String startTab = extras.getString("START_TAB");
			Intent intent = null; 
			
			if(startTab.equals("settings")){
				intent = new Intent(LoginActivity.this, SettingsActivity.class);
			}
			else if(startTab.equals("editAccount")){
				intent = new Intent(LoginActivity.this, EditAccountActivity.class);
			}
			else{
				intent = new Intent(LoginActivity.this, TabRoot.class);
			}
			intent.putExtra("START_TAB", startTab);
			
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			
			
			LoginActivity.this.startActivity(intent);
		}
		else{
			Toast.makeText(LoginActivity.this, "Unable to Login. Check that your username and password are correct", Toast.LENGTH_LONG).show();
			
		}
	}
	
	
	public void addButtonListeners(){
		
		ImageView continueButton = (ImageView) findViewById(R.id.continueButton);
		
		continueButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {
				
				//get info
				EditText usernameText = (EditText) findViewById(R.id.loginUsernameValue);
				EditText passwordText = (EditText) findViewById(R.id.loginPasswordValue);
				
				CheckBox rememberMeCheck = (CheckBox) findViewById(R.id.rememberMeCheckBox);
				
				
				String username = usernameText.getText().toString();
				String password = passwordText.getText().toString();
				
				
				
				
				/** remember me if checked **/
				boolean rememberMe = rememberMeCheck.isChecked();
				
				//call login
				LoginActivity.this.webHelper.login(username, password, rememberMe);
				
				if(rememberMe) LoginActivity.this.dbHelper.setBoolean(DBHelper.COLUMN_REMEMBER_ME, 1);
				else LoginActivity.this.dbHelper.setBoolean(DBHelper.COLUMN_REMEMBER_ME, 0);
				

				
			}
			
		});
		
	}

}
